package com.zdb.order.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdb.order.common.PageResult;
import com.zdb.order.entity.User;
import com.zdb.order.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service

public class UserService {
    @Autowired
    private UserMapper userMapper;

    public PageResult<User> queryUserPage(String page, String limit, String sort, String name, String qq, String phone, List<Date> dates) {
        //开始分页
        IPage<User> userPage = new Page<>(Integer.parseInt(page),Integer.parseInt(limit));

        //过滤
        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        User user = new User();

        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("name", name);
//            user.setName(name);
        }
        if (!StringUtils.isEmpty(qq)) {
            wrapper.eq("qq", qq);
//            user.setQq(qq);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
//            user.setPhone(phone);
        }

//        wrapper.setEntity(user);

        //时间范围过滤
        if(!StringUtils.isEmpty(dates)){
            wrapper.between("start_time", dates.get(0),dates.get(1));
        }
        //排序
        //查询
        Page<User> pageInfo = (Page<User>)userMapper.selectPage(userPage, wrapper);
        System.out.println("ceshi:"+pageInfo.toString());

        return new PageResult<User>((long) pageInfo.getTotal(), (ArrayList<User>) pageInfo.getRecords());
    }

    public int editRowUser(User editObject) {
        int count  = userMapper.updateById(editObject);
        return count;
    }

    public int deleteRowUser(User deleteObject) {
        int count = userMapper.delete(new QueryWrapper<User>().setEntity(deleteObject));
        return count;
    }
}
